# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=srt
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.5.2
pkgrel=1
pkgdesc="Secure Reliable Transport (SRT) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('spdx:MPL-2.0')
url="https://www.srtalliance.org/"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/Haivision/srt/archive/v${pkgver}.tar.gz"
        0006-no-msvc-compat-headers.patch)
sha256sums=('463970a3f575446b3f55abb6f323d5476c963c77b3c975cd902e9c87cdd9a92c'
            '18af8a55e898b12074e797a212eda5e6f3c935dcc37745eb97a60591e80ab64e')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xzf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true

  cd ${_realname}-${pkgver}

  patch -p1 -i ${srcdir}/0006-no-msvc-compat-headers.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ${_extra_config[@]} \
      -DENABLE_SHARED=ON \
      -DENABLE_STATIC=ON \
      -DENABLE_RELATIVE_LIBPATH=ON \
      -DUSE_ENCLIB=openssl \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

package() {
  # Shared Install
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  local _WIN_PREFIX=$(cygpath -am ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -e "s|${_WIN_PREFIX}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
